﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using THEDARKKNIGHT.UI;
using THEDARKKNIGHT.EventSystem;
using THEDARKKNIGHT.FileSystem;
using System;
using UnityEngine.UI;
using System.Text;
using THEDARKKNIGHT.ConstDefine;

public class DeviceListWindows : BBaseWindows {


    private GameObject ReUIRoot;

    private GameObject DeviceItem;

    private Transform GridLayoutParent;

    private Dictionary<string, DeviceInfo> DeviceDic;

    private Color OnLineFlag = new Color(70, 97, 255);

    private Color OffLineFlag = new Color(180, 186, 216);

    private const string DEIVECFILE = "DEVICELIST";

    public DeviceListWindows(string windowsName) {
        this.WindowsAlias = windowsName;
    }

    public override void AddListener()
    {
        BEventManager<object>.Instance().AddListener(UIEventDefine.CLIENTONLINE, ClientOnLineCallback);
        BEventManager<object>.Instance().AddListener(UIEventDefine.CLIENTOFFLINE, ClientOffLineCallback);
    }

    private object ClientOffLineCallback(object data)
    {
        if (data != null) {
            try {
                DeviceInfo deviceInfo = (DeviceInfo)data;
                DeviceInfo deviceItem = null;
                if (DeviceDic.TryGetValue(deviceInfo.DeviceName, out deviceItem)) {
                    deviceItem.DeviceNameUI.text = deviceInfo.DeviceName;
                    deviceItem.DeviceStatusUI.color = deviceInfo.DeviceStatus ? OnLineFlag : OffLineFlag;
                }
            } catch (Exception ex) {
                Debug.LogError(ex.Message);
            }


        }
        return null;
    }

    private object ClientOnLineCallback(object data)
    {
        if (data != null)
        {
            try
            {
                DeviceInfo deviceInfo = (DeviceInfo)data;
                GameObject device = GameObject.Instantiate(DeviceItem, GridLayoutParent);
                deviceInfo.DeviceNameUI = device.transform.Find("DeviceName").GetComponent<Text>();
                deviceInfo.DeviceStatusUI = device.transform.Find("DeviceMark").GetComponent<Image>();
                deviceInfo.DeviceNameUI.text = deviceInfo.DeviceName;
                deviceInfo.DeviceStatusUI.color = deviceInfo.DeviceStatus ? OnLineFlag : OffLineFlag;
                DeviceDic.Add(deviceInfo.DeviceName, deviceInfo);
            }
            catch (Exception ex) {
                Debug.LogError(ex.Message);
            }
        }
        return null;
    }

    public override void DataInit()
    {
        ReUIRoot = Resources.Load<GameObject>("UIPrefabs/DeviceListPanel");
        DeviceItem = Resources.Load<GameObject>("UIPrefabs/DeviceItem");
        LoadDeviceDataFromDisk();
    }

    private void LoadDeviceDataFromDisk()
    {
        try
        {
            byte[] data = BFileSystem.Instance().ReadFileFromDisk(BFameWorkPathDefine.BFameDataPath, DEIVECFILE);
            string DeviceJson = Encoding.UTF8.GetString(data);
            DeviceDic = JsonUntity.DeserializeStringToDictionary<string, DeviceInfo>(DeviceJson);
        }
        catch (Exception ex) {
            DeviceDic = new Dictionary<string, DeviceInfo>();
        }

    }

    public override void DestoryWindows()
    {
        string DeivceData = JsonUntity.SerializeDictionaryToJsonString<string, DeviceInfo>(DeviceDic);
        BFileSystem.Instance().WriteFileToDiskAsync(Encoding.UTF8.GetBytes(DeivceData), BFameWorkPathDefine.BFameDataPath, DEIVECFILE,true);
        ReUIRoot = null;
    }

    public override bool GetWindowsMsg(int windowsID, string windowsAlias, object data)
    {
        return true;
    }

    public override void Hide()
    {
        
    }

    public override void RemoveListener()
    {
        
    }

    public override void Show()
    {
        
    }

    public override Transform UIInit(Transform UIRoot)
    {
        GameObject rootUI = GameObject.Instantiate(ReUIRoot,UIRoot);
        GridLayoutParent = rootUI.transform.Find("DeviceList/Viewport/Content");
        return rootUI.transform;
    }

    [Serializable]
    public class DeviceInfo {

        public string DeviceName;
        public bool DeviceStatus;

        [NonSerialized]
        public Text DeviceNameUI;
        [NonSerialized]
        public Image DeviceStatusUI;
    }
}
